package com.call.data;


import com.call.data.MissCallData.TableColumns;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DataBaseHelper extends SQLiteOpenHelper {

	public DataBaseHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
		// TODO Auto-generated constructor stub
	}
	
	static final String dbName="demoDB";
	
	static final String MissSMS_Table="MissSMS";
	static final String colMissSMS_ID="MissSMS_ID";
	static final String colMissSMS_Time="MissSMS_Time";
	static final String colMissSMS_PhoneNumber = "MissSMS_PhoneNumber";
	static final String colMissSMS_Messager = "MissSMS_Messager";
	
	static final String MissCall_Table="MissCALL";
	static final String colMissCALL_ID="MissCALL_ID";
	static final String colMissCALL_PhoneNumber="MissCALL_PhoneNumber";
	static final String colMissCALL_Time="MissCALL_Time";
	
	public DataBaseHelper(Context context) {
		// TODO Auto-generated constructor stub
		super(context, dbName, null,33);
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		Log.v("DatabaseHelper","onCreate");
		
		db.execSQL("DROP TABLE IF EXISTS "+ MissSMS_Table);
		db.execSQL("DROP TABLE IF EXISTS "+ MissCall_Table);
		
		db.execSQL("CREATE TABLE " + MissSMS_Table + " ("
         		+ colMissSMS_ID + " INTEGER PRIMARY KEY,"
                 + colMissSMS_Time + " TEXT,"
                 + colMissSMS_PhoneNumber + " TEXT,"
                  + colMissSMS_Messager + "  TEXT "
                 + ");");
		
		 db.execSQL("CREATE TABLE " + MissCall_Table + " ("
         		+ colMissCALL_ID + " INTEGER PRIMARY KEY,"
                 + colMissCALL_Time + " TEXT,"
                 + colMissCALL_PhoneNumber + " TEXT"
                 + ");");
		 
		 
		//Inserts pre-defined departments
		//InsertDepts(db);
	}
	
	
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		Log.v("DatabaseHelper","onUpgrade");
		db.execSQL("DROP TABLE IF EXISTS "+ MissSMS_Table);
		db.execSQL("DROP TABLE IF EXISTS "+ MissCall_Table);
		
		//db.execSQL("DROP TRIGGER IF EXISTS dept_id_trigger");
		//db.execSQL("DROP TRIGGER IF EXISTS dept_id_trigger22");
		//db.execSQL("DROP TRIGGER IF EXISTS fk_empdept_deptid");
		//db.execSQL("DROP VIEW IF EXISTS "+viewEmps);
		onCreate(db);
	}
	
	 public void AddSMS(MissSMS sms)
		{
			 Log.v("DatabaseHelper","AddSMS");
			 
			 SQLiteDatabase db= this.getWritableDatabase();
			 
			
			ContentValues cv=new ContentValues();
			
			Log.v("AddSMS",colMissSMS_Messager + "  " + sms.getMessager() );
			
			cv.put(colMissSMS_Time, sms.getTime());
			cv.put(colMissSMS_Messager, sms.getMessager());
			cv.put(colMissSMS_PhoneNumber, sms.getNumberPhone());
//			
			//cv.put(colDept,2);
			
			db.insert(MissSMS_Table, "", cv);
			db.close();

		}
	
	 
	 public void getSMSCount()
	 {
		
			SQLiteDatabase db=this.getWritableDatabase();
			Cursor cur= db.rawQuery("Select * from "+ MissSMS_Table, null);
			int x= cur.getCount();
			cur.close();
			
			 Log.v("DatabaseHelper","getSMSCount " + String.valueOf(x));
			
			//return x;
	 }
	//static final String viewEmps="ViewEmps";
	
}
